//
// Created by Eric on 2022/11/5.
// 管理Event数据的类
//

#ifndef UUU_EVENT_H
#define UUU_EVENT_H
class Event {
public:
    // 事件类型
    enum EventType{
        Null, // 空类型
        Detect, // 侦测
        War, // 战争
        Develop // 发展
    };
    // 携带的一些特殊状态
    enum State:unsigned long long{
        None = 0,  // 无特殊状态
        Colonize = 0b00000001, // 殖民运动
        Jump     = 0b10000000,
    };

    typedef void (*p_process_func)(int idx1, int idx2);

public:
    // api
    // 该函数用于比较两个事件之间的优先级
    [[nodiscard]]bool Prefer(const Event&ahs) const;

    // 添加状态
    void AddState(State new_state) {
        state_ |= new_state;
    }

    // 消除状态
    void SubState(State target_state){
        state_ &= ~target_state;
    }


    void Display() const {
        cout << "source_cz_idx: " << source_cz_idx_ << endl;
        cout << "target_cz_idx: " << target_cz_idx_ << endl;
        string type;
        switch (type_) {
            case Null:
                type = "Null";
                break;
            case War:
                type = "War";
                break;
            case Detect:
                type = "Detect";
                break;
            case Develop:
                type = "Develop";
                break;
            default:
                break;
        }
        cout << "event type: " << type << endl;

        cout << "event state: ";
        if (state_ | Colonize) {
            cout << "colonize ";
        }
        if (state_ | Jump) {
            cout << "jump ";
        }
        cout << endl;
    }

public:
    // 构造方法
    Event(int source_cz_idx, int target_cz_idx, EventType type, int priority);
    Event();

public:
    // getter & setter
    [[nodiscard]]int source_cz_idx()const {return source_cz_idx_;}
    [[nodiscard]]int target_cz_idx()const {return target_cz_idx_;}
    [[nodiscard]]EventType type() const{return type_;}
    void SetType(Event::EventType type){
        type_ = type;
    }
    [[nodiscard]]unsigned long long state()const {return state_;}

    void SetTargetCzIdx(int target_cz_idx) {
        target_cz_idx_ = target_cz_idx;

    }
    void SetState(State state){
        state_ = state;
    }


private:
    // data
    // Event携带的一些信息
    int source_cz_idx_; // 发起文明索引
    int target_cz_idx_; // 接受文明索引
    EventType type_; // 事件类型
    int priority_;
    unsigned long long state_;
};

Event::Event() {
    source_cz_idx_ = -1;
    target_cz_idx_ = -1;
    type_ = EventType::Null;
    priority_ = 0;
    state_ = Event::State::None;
}

Event::Event(int source_cz_idx, int target_cz_idx, EventType type, int priority):source_cz_idx_(source_cz_idx), target_cz_idx_(target_cz_idx), type_(type), priority_(priority){
    state_ = Null;
}

/**
 * @brief 用于比较两个事件的执行优先级
 * @param ahs
 * @return
 */

bool Event::Prefer(const Event &ahs) const {
    if(type_ != ahs.type()){
        return false;
    }
    if(type_ == ahs.type()){
        switch (type_) {
            case Null:
                return false;
                break;
            case Develop:
                return false;
                break;
            case Detect:
                return false;
                break;
            case War:
                return true;
                break;
            default:
                return true;
        }
    }
    // TODO 完善这里的逻辑
    return false;
}

#endif //UUU_EVENT_H
